home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Games / Hexagonal CA / HexCA.c / Shell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-30  |  6.1 KB  |  255 lines  |  [TEXT/CWIE]

  1. #define _Main_Module_
  2.  
  3. #include <Palettes.h>
  4. #include <Profiler.h>
  5. #include "AppGlobals.h"
  6. #include "AppTypes.h"
  7. #include "AppConstants.h"
  8. #include "AppMacros.h"
  9. #include "DrawGridUtilPr.h"
  10. #include "TablesUtilPr.h"
  11. #include "ToolTrapsPr.h"
  12. #include "NewDialogPr.h"
  13.  
  14. #include "ShellPr.h"
  15.  
  16. static void        InitToolbox            ( void );
  17. static Boolean        InitMemory            ( void );
  18. static WindowRef    InitWindow            ( void );
  19. static Boolean        SetWindPaletteFromClut    ( WindowRef, CTabHandle, short );
  20. static Boolean        InitGrid                ( WindowRef );
  21. static void        RandomizeGrid            ( GridDataPtr, Boolean );
  22.  
  23. // main -    the program entry/exit point
  24. void main( void )
  25. {            
  26.     // start up the profiler
  27. #if __profile__
  28.     if( ProfilerInit( collectDetailed, bestTimeBase, 100, 10 ) != noErr )
  29.         Debugger();
  30. #endif
  31.  
  32.     InitToolbox();
  33.     {
  34.         short    depthMode;
  35.         
  36.         gSaveGDevice = GetMainDevice();
  37.         gSaveDepth = HasDepth( gSaveGDevice, (**((**gSaveGDevice).gdPMap)).pixelSize, 1, 1 );
  38.         if( depthMode = HasDepth( gSaveGDevice, 8, 1, 1 ) )
  39.             SetDepth( gSaveGDevice, depthMode, 1, 1 );
  40.         else
  41.         {
  42.             Alert( k8BitColorsID, nil );
  43.             return;
  44.         }
  45.     }
  46. #ifndef powerc
  47.     GetCommonTraps();
  48. #endif
  49.     if( InitMemory() )
  50.     {
  51.         WindowRef    theWindow;
  52.         Boolean        allGood = true;
  53.                 
  54.         while( (theWindow = InitWindow()) && allGood )
  55.         {            
  56.             FlushEvents( everyEvent, 0 );
  57.             if( allGood = InitGrid( theWindow ) )
  58.             {
  59.                 while( !MButton() )
  60.                     calculateNextIteration( theWindow );
  61.                     
  62.                 DisposeTables( (GridDataPtr)MGetWRefCon( theWindow ) );
  63.                 UnlockPixels( MGetGWorldPixMap( ((GridDataPtr)MGetWRefCon( theWindow ))->theGWorld ) );
  64.                 DisposePtr( (Ptr)MGetWRefCon( theWindow ) );
  65.                 DisposeWindow( theWindow );
  66.             }
  67.         }
  68.     }
  69.     SetDepth( gSaveGDevice, gSaveDepth, 1, 1 );
  70.  
  71.     // shut down the profiler
  72. #if __profile__
  73.     ProfilerDump( "\pHexCA.prof" );
  74.     ProfilerTerm();
  75. #endif
  76. }
  77.  
  78. // InitToolbox -    call to initialize the Mac toolboxes
  79. static void InitToolbox( void )
  80. {
  81.     InitGraf( &qd.thePort );
  82.     InitFonts();
  83.     InitWindows();    
  84.     InitMenus();
  85.     InitCursor();
  86.     TEInit();
  87.     InitDialogs( 0L );
  88.     GetDateTime( (unsigned long *)&qd.randSeed );
  89. }
  90.  
  91. // InitMemory -    call to initialize the memory
  92. static Boolean InitMemory( void )
  93. {
  94.     if( !(gClut = GetCTable( kClutTableID )) )
  95.     {
  96.         Alert( kNotEnoughMemoryID, nil );
  97.         return( false );
  98.     }
  99.     if( !(gDeskMenu = GetMenu( kAppleMenuID )) )
  100.     {
  101.         Alert( kNotEnoughMemoryID, nil );
  102.         return( false );
  103.     }
  104.     AddResMenu( gDeskMenu, 'DRVR' );
  105.     if( !(gFileMenu = GetMenu( kFileMenuID )) )
  106.     {
  107.         Alert( kNotEnoughMemoryID, nil );
  108.         return( false );
  109.     }
  110.     if( !(gEditMenu = GetMenu( kEditMenuID )) )
  111.     {
  112.         Alert( kNotEnoughMemoryID, nil );
  113.         return( false );
  114.     }
  115.     return( true );
  116. }
  117.  
  118. // InitWindow -    call to create a window over the entire screen
  119. static WindowRef InitWindow( void )
  120. {
  121.     WindowRef    theWindow;
  122.     GrafPtr        savePort;
  123.     short        mBarHeight;
  124.     Rect            winRect = qd.screenBits.bounds;
  125.     
  126.     InsertMenu( gDeskMenu, 0 );
  127.     InsertMenu( gFileMenu, 0 );
  128.     InsertMenu( gEditMenu, 0 );
  129.     InvalMenuBar();
  130.     
  131.     mBarHeight = GetMBarHeight();
  132.     MInsetRect( winRect, 4, 4 );
  133.     winRect.top += mBarHeight;
  134.     
  135.     if( !(theWindow = NewCWindow( nil, &winRect, "\p", false, plainDBox, (WindowRef)-1L, false, 0L )) )
  136.     {
  137.         Alert( kNotEnoughMemoryID, nil );
  138.         return( nil );
  139.     }
  140.     GetPort( &savePort );
  141.     SetPort( theWindow );
  142.     
  143.     if( !SetWindPaletteFromClut( theWindow, gClut, 0x0000 ) )
  144.     {
  145.         DisposeWindow( theWindow );
  146.         return( nil );
  147.     }
  148.     
  149.     TextFont( geneva );
  150.     TextSize( 10 );
  151.     
  152.     SetPort( savePort );
  153.     
  154.     return( theWindow );
  155. }
  156.  
  157. // SetWindPaletteFromClut -    Sets the given window's palette to the given colour table
  158. static Boolean SetWindPaletteFromClut( WindowRef theWindow, CTabHandle theClut, short tolerence )
  159. {
  160.     PaletteHandle        thePalette = nil;
  161.  
  162.     if( !(thePalette = GetPalette( theWindow )) )
  163.     {
  164.         if( !(thePalette = NewPalette( (**theClut).ctSize + 1, theClut, pmTolerant + pmExplicit, tolerence )) )
  165.         {
  166.             Alert( kNotEnoughMemoryID, nil );
  167.             return( false );
  168.         }
  169.     }
  170.     else
  171.     {
  172.         CTab2Palette( theClut, thePalette, pmTolerant + pmExplicit, tolerence );
  173.         if( !thePalette )
  174.         {
  175.             Alert( kNotEnoughMemoryID, nil );
  176.             return( false );
  177.         }
  178.     }
  179.         
  180.     NSetPalette( theWindow, thePalette, pmAllUpdates );
  181.         
  182.     ActivatePalette( theWindow );
  183.     
  184.     return( true );
  185. }
  186.  
  187. // InitGrid -    call to initialize the grid for a window
  188. static Boolean InitGrid( WindowRef theWindow )
  189. {
  190.     GridDataPtr    refCon;
  191.     Boolean        seedCenter;
  192.     
  193.     if( !(refCon = (GridDataPtr)NewPtr( sizeof( GridDataRec ) )) )
  194.     {
  195.         Alert( kNotEnoughMemoryID, nil );
  196.         return( false );
  197.     }
  198.     MSetWRefCon( theWindow, (long)refCon );
  199.     
  200.     if( !doNewDialog( refCon, &seedCenter ) )
  201.         return( false );
  202.     ClearMenuBar();
  203.     DrawMenuBar();
  204.     ShowWindow( theWindow );
  205.     
  206.     refCon->theGWorld = (GWorldPtr)theWindow;    
  207.     
  208.     switch( refCon->scale )
  209.     {
  210.         case kHexSize:
  211.             refCon->gridHSize = (theWindow->portRect.right - kHexSize) / kHexSize;
  212.             refCon->gridVSize = theWindow->portRect.bottom / (kHexMidPoint + kHexMidPoint / 2 + 1);
  213.             break;
  214.         case kMiniHexSize:
  215.             refCon->gridHSize = (theWindow->portRect.right - kMiniHexSize) / kMiniHexSize;
  216.             refCon->gridVSize = theWindow->portRect.bottom / (kMiniHexMidPoint + kMiniHexMidPoint / 2 + 1);
  217.             break;
  218.         case kTinyHexSize:
  219.             refCon->gridHSize = (theWindow->portRect.right - kTinyHexSize) / kTinyHexSize;
  220.             refCon->gridVSize = theWindow->portRect.bottom / (kTinyHexMidPoint + kTinyHexMidPoint / 2 + 1);
  221.             break;
  222.     }
  223.  
  224.     if( refCon->gridHSize % 2 ) refCon->gridHSize--;
  225.     if( refCon->gridVSize % 2 ) refCon->gridVSize--;
  226.     refCon->gridTotalSize = refCon->gridHSize * refCon->gridVSize;
  227.     refCon->iterationString[0] = 1;
  228.     refCon->iterationString[1] = '0';
  229.     
  230.     LockPixels( MGetGWorldPixMap( refCon->theGWorld ) );
  231.     if( !InitWindowTables( theWindow ) )
  232.     {
  233.         UnlockPixels( MGetGWorldPixMap( refCon->theGWorld ) );
  234.         DisposePtr( (Ptr)refCon );
  235.         Alert( kNotEnoughMemoryID, nil );
  236.         return( false );
  237.     }
  238.     RandomizeGrid( refCon, seedCenter );
  239.     drawHexGrid( theWindow );
  240.     return( true );
  241. }
  242.  
  243. // RandomizeGrid -    call to randomize the grid
  244. static void RandomizeGrid( GridDataPtr refCon, Boolean seedCenter )
  245. {
  246.     if( seedCenter )
  247.         *(refCon->oldGrid + (refCon->gridTotalSize + refCon->gridHSize) / 2) = 1;
  248.     else
  249.     {
  250.         long        count = 0;
  251.         
  252.         for( count = 0; count < refCon->gridTotalSize; count++ )
  253.             *(refCon->oldGrid + count) = MRandom() > 16000;
  254.     }
  255. }